
var netUtils = require('../../../utils/netUtils.js');
var bsurl = require('../../../utils/baseUrl.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    orders: [],
    url: "order/queryOrderByState.do",
    buyerStr: [],
    hidden: false,
    hiddenComm: true,
    hiddenComm1: true,
    score: "",
    comment: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var str = [["待付款", "付款"], ["待发货", ""], ["已发货", "收货"], ["待评价", "评价"], ["已完成", ""]];
    if (app.globalData.identity) {
      app.editTabBar(app.globalData.tabBar);
      this.setData({ hidden: true })
      str = [["待付款", ""], ["待发货", "发货"], ["待收货", ""], ["待评价", ""], ["已完成", ""]];
    }
    var tab = 0;
    if (options.currentTab) { tab = options.currentTab }
    this.setData({
      buyerStr: str,
      currentTab: tab,
      identity: app.globalData.identity
    });
    this.queryByStatus(this.data.currentTab);
  },
  //tab栏点击
  switchtab: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.t
    })
    this.queryByStatus(this.data.currentTab);
  },
  //每个订单的处理按钮
  deal: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var body = {};
    if (app.globalData.identity) {
      //是卖家
      body = { id: id, status: 2 };
    }
    else {
      //是买家
      if (this.data.currentTab == 0) {
        body = { id: id, status: 1 };
      } else if (this.data.currentTab == 2) {
        body = { id: id, status: 3 };
      } else if (this.data.currentTab == 3) {
        this.orderComm(e.currentTarget.dataset);
        return;
      }
    }
    var that = this;
    netUtils.sendRequest("order/changeOrderStatus.do", body, (data) => {
      wx.showToast({
        title: this.data.buyerStr[this.data.currentTab][1] + '成功',
        icon: 'success',
        duration: 1500
      });
      that.data.orders.splice(index, 1);
      that.setData({ orders: that.data.orders });
    })
  },
  //order评价,评价卖家
  orderComm: function (dataset) {
    console.log(dataset)
    var body = {
      shopname: dataset.shopname,
      username: app.globalData.userInfo.name1,
      score: 8,
      comments: "好好好",
      //praise存订单id
      praise: dataset.id
    }
    this.setData({ hiddenComm: false, hiddenComm1: false, body: body, dataset: dataset });

  },
  //物品评价按钮
  btnComm: function (e) {
    var dataset = e.currentTarget.dataset;
    var body = {
      goodsid: dataset.goodsid,
      username: app.globalData.userInfo.name1,
      comments: "",
      //praise存物品订单id
      praise: dataset.id
    }
    this.setData({ hiddenComm: false, body: body,dataset:dataset });

  },
  queryByStatus: function (status) {
    var that = this;
    var body = { state: status };
    if (this.data.identity) {
      body = { state: status, seller: app.globalData.userInfo.username }
    }
    console.log(app.globalData)
    console.log(status)
    console.log(body)
    netUtils.sendRequest(that.data.url, body, (data) => {
      that.setData({ orders: data.body });
      console.log(that.data.orders);
    });
  },
  //弹框取消按钮
  cancel: function () {
    this.setData({ hiddenComm: true, hiddenComm1: true });
  },
  //弹框评价按钮
  confirm: function () {
    console.log(this.data.shopname)
    if (!this.data.comment) {
      wx.showModal({
        title: '提示',
        content: '评论不能为空',
      });
      return;
    }
    var that = this;
    var dataset = this.data.dataset;
    if (this.data.hiddenComm1) {
      that.data.body.comments = that.data.comment;
      netUtils.sendRequest("comment/addGoodsComment.do", that.data.body, () => {
        console.log(dataset)
        that.data.orders[dataset.orderindex].goodsList[dataset.index].commentstatus = true;
        that.setData({ orders: that.data.orders });
      })
    } else {
     
      that.data.body.score = Number(that.data.score);
      if (!that.data.body.score || that.data.body.score < 0 || that.data.body.score > 10) {
        wx.showModal({
          title: '提示',
          content: '评分有误，分数为0到10',
        });
        return;
      }
      that.data.body.comments = that.data.comment;
      netUtils.sendRequest("comment/addSellerComment.do", that.data.body, () => {
        that.data.orders.splice(dataset.index,1);
        that.setData({ orders: that.data.orders });
      })
    }
    this.setData({ hiddenComm: true, hiddenComm1: true });
  },
  //弹框输入框
  input: function (e) {
    if (e.currentTarget.dataset.type == 1) {
      this.setData({ score: e.detail.value });
    } else {
      this.setData({ comment: e.detail.value });
    }
  }
})